import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../resources.dart';
import 'base_model.dart';

/// created by hcl
/// on 2025/4/27
/// page BaseImageState
/// desc 背景图做背景的页面
abstract class BaseImageState<T extends BaseModel, P extends StatefulWidget>
    extends State<P> {
  final FocusNode _blankNode = FocusNode();

  late T model;

  T initModel();

  @protected
  Widget appBarWidget() {
    return Container(
      width: designWidth.ss,
      clipBehavior: Clip.hardEdge,
      decoration: BoxDecoration(
          color: ColorUtils.app_color.withOpacity(0.3),
          borderRadius: BorderRadius.only(
              bottomLeft: Radius.circular(25.ss),
              bottomRight: Radius.circular(25.ss))),
      child: BackdropFilter(
        filter: ImageFilter.blur(sigmaX: 10, sigmaY: 10),
        child: Padding(
          padding: EdgeInsets.only(bottom: 30.ss, left: 13.ss, right: 13.ss),
          child: Column(
            children: [
              SizedBox(height: 72.ss),
              Row(children: [
                InkWell(
                  onTap: () {
                    if (canPop) {
                      Nav.pop(result: null);
                    }
                  },
                  child: SizedBox(
                    width: 25.ss,
                    height: 25.ss,
                    child: assetImage(ImageUtils.back,
                        width: 25.ss, height: 25.ss),
                  ),
                ),
                SizedBox(width: 12.ss),
                Expanded(
                    child: Text(title,
                        style: semiboldF17, textAlign: TextAlign.center)),
                SizedBox(width: 12.ss),
                SizedBox(width: 25.ss),
              ]),
            ],
          ),
        ),
      ),
    );
  }

  @protected
  Widget bodyWidget(BuildContext context);

  ///是否显示appBar
  @protected
  bool get showAppBar => true;

  ///是否 resizeToAvoidBottomInset 软键盘相关
  @protected
  bool get bottomInset => false;

  @protected
  String get title => '';

  @protected
  Color get backColor => ColorUtils.color_4D4170;

  @protected
  String get backImage => '';

  @protected
  bool get canPop => true;

  @override
  @protected
  void initState() {
    model = initModel();
    model.mContext = context;
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((callback) {
      model.initData();
    });
  }

  @override
  @protected
  void dispose() {
    // model.dispose();
    super.dispose();
  }

  void hideKeyboard() {
    FocusScope.of(context).requestFocus(_blankNode);
  }

  @override
  Widget build(BuildContext context) {
    return PopScope(
      canPop: canPop,
      child: ChangeNotifierProvider<T>(
        create: (context) => model,
        child: Scaffold(
          resizeToAvoidBottomInset: bottomInset,
          backgroundColor: backColor,
          body: Center(
            child: Container(
              width: designWidth.ss,
              alignment: Alignment.topCenter,
              decoration: backImage.isNotEmpty
                  ? BoxDecoration(
                      image: DecorationImage(
                          image: assetImage(backImage).image, fit: BoxFit.fill))
                  : null,
              child: GestureDetector(
                onTap: () {
                  hideKeyboard();
                },
                child: Selector<T, StateEnum>(
                  selector: (context, model) {
                    return model.state;
                  },
                  builder: (context, state, parent) {
                    return Column(
                      children: [
                        showAppBar ? appBarWidget() : const SizedBox(),
                        Expanded(
                          child: bodyWidget(context),
                        ),
                      ],
                    );
                  },
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}
